package com.demo.controller;

import com.bootlever.framework.fastdfs.templates.FastdfsTemplate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping(value = "/fastdfs")
public class FastdfsController {

    private static final Logger logger = LoggerFactory.getLogger(FastdfsController.class);

    @Autowired
    private FastdfsTemplate fastdfsTemplate;

    @RequestMapping(value = "/upload")
    public String upload() throws Exception {
        String address = fastdfsTemplate.upload("C:\\D\\1.txt");
        //String address = fastdfsTemplate.upload("C:\\D\\4.bat");
        logger.info("文件路径：{}", address);
        return "success";
    }

    @RequestMapping(value = "/download")
    public String dowload() throws Exception {
        InputStream inputStream = fastdfsTemplate.download("group1/M00/00/00/wKiFhFqMF-SADkjOAAAJQS6yZXo466.txt");
        File file = new File("C:\\D\\5.txt");
        byte[] bytes = IOUtils.toByteArray(inputStream);
        FileUtils.writeByteArrayToFile(file, bytes);
        logger.info("下载");
        return "success";
    }

    @RequestMapping(value = "/modify")
    public String modify() throws Exception {
        String address = fastdfsTemplate.modify("group1/M00/00/00/wKiFhFqMF-SADkjOAAAJQS6yZXo466.txt","C:\\D\\2.txt");
        logger.info("新文件：{}", address);
        return "success";
    }

    @RequestMapping(value = "/delete")
    public String delete() throws Exception {
        int result = fastdfsTemplate.delete("group1/M00/00/00/wKiFhFqMKguAN9XJAAAJQS6yZXo856.txt");
        logger.info("删除：{}", result);
        return "success";
    }
}
